from rest_framework_jwt.settings import api_settings
import json
from workorder.settings import ALY_ACCESSKEY_ID,ALY_ACCESSKEY_SECRET
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest







def create_token(user):
    # 生成jwt_token, user对象，查询结果
    jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
    jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
    payload = jwt_payload_handler(user)
    token = jwt_encode_handler(payload)
    return token





def send_sms(phone, data):
    client = AcsClient(ALY_ACCESSKEY_ID, ALY_ACCESSKEY_SECRET, 'cn-hangzhou')

    # data ={ "code" : 123456 }

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', phone)
    request.add_query_param('SignName', "ABC商城")
    request.add_query_param('TemplateCode', "SMS_205885613")
    request.add_query_param('TemplateParam', data)

    response1 = client.do_action(request)
    # python2:  print(response)
    res = json.loads(str(response1, encoding='utf-8'))

    if res["Code"] == "OK":
        return True
    else:
        return False
